package com.neuedu.web.servlet.user; /**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-19 10:03
 * 描述 : ${dc}
 */

import com.neuedu.web.dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 使用地址栏传参 method  为 GET 类型
 * http://localhost:8080/web/userDelete ?  参数列表....... & 作为分隔符
 * http://localhost:8080/web/userDelete?id=1&name=jshand&age=111
 * http://localhost:8080/web/userDelete?id=1
 */
@WebServlet(name = "UserDeleteServlet", value = "/userDelete")
public class UserDeleteServlet extends HttpServlet {
    UserDao  userDao = new UserDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收一个用户id
        String  id = request.getParameter("id");

        // 调用UserDao的删除
        int count = 0;
        try {
            count = userDao.deleteById(Integer.parseInt(id));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("count = " + count);


        //重定向到UserList
        response.sendRedirect(request.getContextPath() +"/userlist");
    }

}
